package com.zhouxiaoge.filters;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.StringUtils;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 周明阳
 * mail:gqzmy@outlook.com
 * 时间:2018-04-09 11:04
 */

public class AnyRolesFilter extends AccessControlFilter {
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue){
        String[] roles = (String[]) mappedValue;
        if (roles == null) {
            /*如果没有设置角色参数，默认成功*/
            return true;
        }
        for (String role : roles) {
            if (getSubject(request, response).hasRole(role)) {
                return true;
            }
        }
        /*跳到onAccessDenied处理*/
        return false;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        Subject subject = getSubject(request, response);
        /*表示没有登录，重定向到登陆页面*/
        if (subject.getPrincipal() == null) {
            saveRequest(request);
            String loginUrl = "/login.jsp";
            WebUtils.issueRedirect(request, response, loginUrl);
            return false;
        } else {
            String unauthorizedUrl = "/unauthorized.jsp";
            /*如果有未授权页面跳转过去*/
            if (StringUtils.hasText(unauthorizedUrl)) {
                WebUtils.issueRedirect(request, response, unauthorizedUrl);
            } else {
                /*否则返回401未授权状态码*/
                WebUtils.toHttp(response).sendError(HttpServletResponse.SC_UNAUTHORIZED);
            }
        }
        return false;
    }
}
